package cs.ui250.carnatic;

/* NOTES
 * 
 * -Finished Raga Class for now
 * -Trimmed mp3 files
 * -Changed data file to read in numbers instead of swara notes 
 *   (each number correlates to one of the 16 swaras)
 * -RagaBySwara
 * 		*plays notes when toggling ON
 * 		*cannot select equivalent swaras
 * 		*only allowed 7 swaras
 * 		*CLEAR BUTTON implemented
 */

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;

public class RagaBySwaraActivity extends Activity {

	private Context pContext; // Local copy of app context
	private DBAdapter mDB;
	private SoundManager pSoundManager;
	private ToggleButton btn_S, btn_R1, btn_R2, btn_R3, btn_G1, btn_G2, btn_G3,
			btn_P, btn_M1, btn_M2, btn_D1, btn_D2, btn_D3, btn_N1, btn_N2,
			btn_N3;
	private int numberToggled = 0;
	private boolean previouslySeven = false;
	private RagaBySwaraActivity pInstance = this;

	private ArrayList<String> mRagas;
	private ArrayList<Integer> mRagaIDs;
	private ArrayAdapter<String> mAdapter;

	private ListView mRagaList;
	private TextView mNoResultsText;

	private final static int RAGA_FROM_BY_SWARA = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.raga_by_swara);

		pContext = getApplicationContext();
		pSoundManager = SoundManager.getInstance(pContext);
		mDB = DBAdapter.getInstance(pContext);

		mRagas = new ArrayList<String>();
		mRagaIDs = new ArrayList<Integer>();
		mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, mRagas);
		mNoResultsText = (TextView) findViewById(R.id.rbs_empty_text);

		mRagaList = (ListView) findViewById(R.id.rbs_raga_list);
		mRagaList.setAdapter(mAdapter);
		mRagaList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				Intent intent = new Intent(RagaBySwaraActivity.this,
						RagaDisplay.class);
				intent.putExtra("RAGA_ID_KEY", mRagaIDs.get(position));
				startActivityForResult(intent, RAGA_FROM_BY_SWARA);
			}
		});

		btn_S = (ToggleButton) findViewById(R.id.rbs_S);
		btn_S.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_S.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(1);
				} else
					numberToggled--;
				setToggles();
			}
		});

		btn_R1 = (ToggleButton) findViewById(R.id.rbs_R1);
		btn_R1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_R1.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(2);
				} else
					numberToggled--;
				setToggles();
			}
		});

		btn_R2 = (ToggleButton) findViewById(R.id.rbs_R2);
		btn_R2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_R2.isChecked()) {
					btn_G1.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(3);
				} else {
					btn_G1.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_R3 = (ToggleButton) findViewById(R.id.rbs_R3);
		btn_R3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_R3.isChecked()) {
					btn_G2.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(4);
				} else {
					btn_G2.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_G1 = (ToggleButton) findViewById(R.id.rbs_G1);
		btn_G1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_G1.isChecked()) {
					btn_R2.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(3);
				} else {
					btn_R2.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_G2 = (ToggleButton) findViewById(R.id.rbs_G2);
		btn_G2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_G2.isChecked()) {
					btn_R3.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(4);
				} else {
					btn_R3.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_G3 = (ToggleButton) findViewById(R.id.rbs_G3);
		btn_G3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_G3.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(5);
				} else
					numberToggled--;
				setToggles();
			}
		});

		btn_M1 = (ToggleButton) findViewById(R.id.rbs_M1);
		btn_M1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_M1.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(6);
				} else
					numberToggled--;
				setToggles();
			}
		});

		btn_M2 = (ToggleButton) findViewById(R.id.rbs_M2);
		btn_M2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_M2.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(7);
				} else
					numberToggled--;
				setToggles();
			}
		});

		btn_P = (ToggleButton) findViewById(R.id.rbs_P);
		btn_P.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_P.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(8);
				} else
					numberToggled--;
				setToggles();
			}
		});

		btn_D1 = (ToggleButton) findViewById(R.id.rbs_D1);
		btn_D1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_D1.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(9);
				} else
					numberToggled--;
				setToggles();
			}
		});

		btn_D2 = (ToggleButton) findViewById(R.id.rbs_D2);
		btn_D2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_D2.isChecked()) {
					btn_N1.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(10);
				} else {
					btn_N1.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_D3 = (ToggleButton) findViewById(R.id.rbs_D3);
		btn_D3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_D3.isChecked()) {
					btn_N2.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(11);
				} else {
					btn_N2.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_N1 = (ToggleButton) findViewById(R.id.rbs_N1);
		btn_N1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_N1.isChecked()) {
					btn_D2.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(10);
				} else {
					btn_D2.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_N2 = (ToggleButton) findViewById(R.id.rbs_N2);
		btn_N2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_N2.isChecked()) {
					btn_D3.setEnabled(false);
					numberToggled++;
					pSoundManager.playSound(11);
				} else {
					btn_D3.setEnabled(true);
					numberToggled--;
				}
				setToggles();
			}
		});

		btn_N3 = (ToggleButton) findViewById(R.id.rbs_N3);
		btn_N3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (btn_N3.isChecked()) {
					numberToggled++;
					pSoundManager.playSound(12);
				} else
					numberToggled--;
				setToggles();
			}
		});

		// Array of Integers 0's and 1's
		// NEED DATABASE INTEGRATION HERE
		Button find = (Button) findViewById(R.id.rbs_find_button);
		find.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				boolean checked = false;
				int[] swarasToSearch = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						0, 0, 0 };

				if (btn_S.isChecked()) {
					swarasToSearch[0] = 1;
					checked = true;
				}
				if (btn_R1.isChecked()) {
					swarasToSearch[1] = 1;
					checked = true;
				}
				if (btn_R2.isChecked()) {
					swarasToSearch[2] = 1;
					checked = true;
				}
				if (btn_R3.isChecked()) {
					swarasToSearch[3] = 1;
					checked = true;
				}
				if (btn_G1.isChecked()) {
					swarasToSearch[4] = 1;
					checked = true;
				}
				if (btn_G2.isChecked()) {
					swarasToSearch[5] = 1;
					checked = true;
				}
				if (btn_G3.isChecked()) {
					swarasToSearch[6] = 1;
					checked = true;
				}
				if (btn_M1.isChecked()) {
					swarasToSearch[7] = 1;
					checked = true;
				}
				if (btn_M2.isChecked()) {
					swarasToSearch[8] = 1;
					checked = true;
				}
				if (btn_P.isChecked()) {
					swarasToSearch[9] = 1;
					checked = true;
				}
				if (btn_D1.isChecked()) {
					swarasToSearch[10] = 1;
					checked = true;
				}
				if (btn_D2.isChecked()) {
					swarasToSearch[11] = 1;
					checked = true;
				}
				if (btn_D3.isChecked()) {
					swarasToSearch[12] = 1;
					checked = true;
				}
				if (btn_N1.isChecked()) {
					swarasToSearch[13] = 1;
					checked = true;
				}
				if (btn_N2.isChecked()) {
					swarasToSearch[14] = 1;
					checked = true;
				}
				if (btn_N3.isChecked()) {
					swarasToSearch[15] = 1;
					checked = true;
				}
				if (checked) {
					updateList(swarasToSearch);
				}
			}
		});

		Button help = (Button) findViewById(R.id.rbs_help_button);
		help.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				RagaBySwaraHelpDialog dialog = new RagaBySwaraHelpDialog(
						pInstance);
				dialog.show();
			}
		});

		Button clear = (Button) findViewById(R.id.rbs_clear_button);
		clear.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				btn_S.setEnabled(true);
				btn_R1.setEnabled(true);
				btn_R2.setEnabled(true);
				btn_R3.setEnabled(true);
				btn_G1.setEnabled(true);
				btn_G2.setEnabled(true);
				btn_G3.setEnabled(true);
				btn_P.setEnabled(true);
				btn_M1.setEnabled(true);
				btn_M2.setEnabled(true);
				btn_D1.setEnabled(true);
				btn_D2.setEnabled(true);
				btn_D3.setEnabled(true);
				btn_N1.setEnabled(true);
				btn_N2.setEnabled(true);
				btn_N3.setEnabled(true);
				btn_S.setChecked(false);
				btn_R1.setChecked(false);
				btn_R2.setChecked(false);
				btn_R3.setChecked(false);
				btn_G1.setChecked(false);
				btn_G2.setChecked(false);
				btn_G3.setChecked(false);
				btn_P.setChecked(false);
				btn_M1.setChecked(false);
				btn_M2.setChecked(false);
				btn_D1.setChecked(false);
				btn_D2.setChecked(false);
				btn_D3.setChecked(false);
				btn_N1.setChecked(false);
				btn_N2.setChecked(false);
				btn_N3.setChecked(false);
				numberToggled = 0;
				mRagaIDs.clear();
				mRagas.clear();
				mAdapter.notifyDataSetChanged();
				mNoResultsText.setText("No Results");
			}
		});
	}// onCreate

	public void setToggles() {
		if (numberToggled == 7) {
			if (!btn_S.isChecked())
				btn_S.setEnabled(false);
			if (!btn_R1.isChecked())
				btn_R1.setEnabled(false);
			if (!btn_R2.isChecked())
				btn_R2.setEnabled(false);
			if (!btn_R3.isChecked())
				btn_R3.setEnabled(false);
			if (!btn_G1.isChecked())
				btn_G1.setEnabled(false);
			if (!btn_G2.isChecked())
				btn_G2.setEnabled(false);
			if (!btn_G3.isChecked())
				btn_G3.setEnabled(false);
			if (!btn_M1.isChecked())
				btn_M1.setEnabled(false);
			if (!btn_M2.isChecked())
				btn_M2.setEnabled(false);
			if (!btn_P.isChecked())
				btn_P.setEnabled(false);
			if (!btn_D1.isChecked())
				btn_D1.setEnabled(false);
			if (!btn_D2.isChecked())
				btn_D2.setEnabled(false);
			if (!btn_D3.isChecked())
				btn_D3.setEnabled(false);
			if (!btn_N1.isChecked())
				btn_N1.setEnabled(false);
			if (!btn_N2.isChecked())
				btn_N2.setEnabled(false);
			if (!btn_N3.isChecked())
				btn_N3.setEnabled(false);
			previouslySeven = true;
		}
		if (numberToggled == 6 && previouslySeven) {
			if (!btn_S.isEnabled())
				btn_S.setEnabled(true);
			if (!btn_R1.isEnabled())
				btn_R1.setEnabled(true);
			if (!btn_R2.isEnabled() && !btn_G1.isChecked())
				btn_R2.setEnabled(true);
			if (!btn_R3.isEnabled() && !btn_R3.isChecked())
				btn_R3.setEnabled(true);
			if (!btn_G1.isEnabled() && !btn_R2.isChecked())
				btn_G1.setEnabled(true);
			if (!btn_G2.isEnabled() && !btn_R3.isChecked())
				btn_G2.setEnabled(true);
			if (!btn_G3.isEnabled())
				btn_G3.setEnabled(true);
			if (!btn_M1.isEnabled())
				btn_M1.setEnabled(true);
			if (!btn_M2.isEnabled())
				btn_M2.setEnabled(true);
			if (!btn_P.isEnabled())
				btn_P.setEnabled(true);
			if (!btn_D1.isEnabled())
				btn_D1.setEnabled(true);
			if (!btn_D2.isEnabled() && !btn_N1.isChecked())
				btn_D2.setEnabled(true);
			if (!btn_D3.isEnabled() && !btn_D3.isChecked())
				btn_D3.setEnabled(true);
			if (!btn_N1.isEnabled() && !btn_D2.isChecked())
				btn_N1.setEnabled(true);
			if (!btn_N2.isEnabled() && !btn_D3.isChecked())
				btn_N2.setEnabled(true);
			if (!btn_N3.isEnabled())
				btn_N3.setEnabled(true);
			previouslySeven = false;
		}
	}

	private void updateList(int[] swaras) {
		mRagaIDs.clear();
		mRagas.clear();
		Cursor cursor = mDB.ragaBySwara(swaras);
		startManagingCursor(cursor);
		if (cursor.moveToFirst()) {
			do {
				mRagaIDs.add(cursor.getInt(0));
				mRagas.add(cursor.getString(1) + " (" + cursor.getInt(0) + ")");
			} while (cursor.moveToNext());
			mNoResultsText.setText("");
		} else {
			mNoResultsText.setText("No Results");
		}
		mAdapter.notifyDataSetChanged();
	}

}// class
